package com.zhuss.core.processor;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.login.wechat.DeleteWxcodeBo;
import com.zhuss.bean.bo.core.user.wechat.UnbindUserOpenIdBo;
import com.zhuss.bean.vo.core.user.DetailUserVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.UserDao;
import com.zhuss.core.db.entity.User;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.IsEnum;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.OceanException;
import com.zhuss.util.JsonUtil;

/**
 * 解绑用户openId processor
 * @Author: zhushanshuo
 * @Date: 2020/03/26 15:51
 */
@Component("userOpenIdUnbind")
public class UserOpenIdUnbind extends AbstractProcessor<Object, UnbindUserOpenIdBo> implements Processor<Object, UnbindUserOpenIdBo> {

    @Autowired
    private UserDao userDao;
    
    @Autowired
	@Qualifier("userRead")
	private UserRead userRead;
    
    @Autowired
    private UserCheck userCheck;
    
    @Autowired
	@Qualifier("wxcodeDelete")
	private Processor<Object, DeleteWxcodeBo> wxcodeDelete;
    
    /**
     * 校验
     */
    @Override
    public void check(UnbindUserOpenIdBo bo) throws OceanException {
    	
    }
    
    /**
     * 操作方法
     * @throws OceanException 
     */
    @Override
    public Object process(UnbindUserOpenIdBo param) throws OceanException {
    	
    	Long userId = param.getUserId();
    	
    	User record = new User();
    	record.setUserId(userId);
		record.setOpenId(null);
		record.setIsBind(IsEnum.是.key());
		
		record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
		userDao.nullOpenId(record);
		
		DeleteWxcodeBo bo = new DeleteWxcodeBo();
		BeanUtils.copyProperties(param, bo);
		bo.setUserId(userId);
		wxcodeDelete.process(bo);
    	
    	String name = ((DetailUserVo) userRead.detail(userId)).getName();
    	
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(UnbindUserOpenIdBo param) throws OceanException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.用户解绑openId.key());
    	record.setTargetId(param.getUserId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
